const { Sequelize, DataTypes } = require('sequelize');
require('dotenv').config();

// 创建 Sequelize 实例
const sequelize = new Sequelize(
  process.env.MYSQL_DATABASE || 'partpark',
  process.env.MYSQL_USER || 'root',
  process.env.MYSQL_PASSWORD || '123456789',
  {
    host: process.env.MYSQL_HOST || 'localhost',
    port: process.env.MYSQL_PORT || 3306,
    dialect: process.env.MYSQL_DIALECT || 'mysql',
    logging: process.env.NODE_ENV === 'development' ? console.log : false,
    pool: {
      max: 10,
      min: 0,
      acquire: 30000,
      idle: 10000
    },
    define: {
      timestamps: true,
      underscored: false,  // 修改为false，保持驼峰命名
      paranoid: true
    }
  }
);

// 测试数据库连接
const testConnection = async () => {
  try {
    await sequelize.authenticate();
    console.log('✅ MySQL database connected successfully');
  } catch (error) {
    console.error('❌ MySQL database connection error:', error);
    process.exit(1);
  }
};

module.exports = {
  sequelize,
  DataTypes,
  testConnection
};